# 第 0006 题： 你有一个目录，放了你一个月的日记，都是txt，为了避免分词的问题，假设内容都是英文，请统计出你认为每篇日记最重要的词。
# 目录：/text
import string
import os


def statistics_text(text_dir):
    word = ''
    word_dict = {}
    inWord = False

    files = os.listdir(text_dir)
    for file in files:
        filepath = text_dir + os.sep + file
        if os.path.isfile(filepath):
            with open(filepath) as f:
                for line in f:
                    for x in line:
                        if x in string.ascii_letters:
                            word = word + x
                            if not inWord:
                                inWord = True
                        else:
                            if inWord:
                                # update dict
                                word_dict[word] = 1 if word_dict.get(word) is None else word_dict[word] + 1
                            inWord = False
                            word = ''
    print(word_dict)


statistics_text(r'resource/text')